package com.yizisu.basemvvm.view

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.yizisu.basemvvm.mvvm.MvvmFragment

fun AppCompatActivity.simpleFragmentPagerAdapter(fg: MutableList<MvvmFragment>): BaseViewPagerAdapter {
    return BaseViewPagerAdapter(supportFragmentManager, fg)
}

fun Fragment.simpleFragmentPagerAdapter(fg: MutableList<MvvmFragment>): BaseViewPagerAdapter? {
    val fm = fragmentManager ?: return null
    return BaseViewPagerAdapter(fm, fg)
}

open class BaseViewPagerAdapter(
    fm: FragmentManager,
    val pagers: MutableList<MvvmFragment>
) : FragmentStatePagerAdapter(
    fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
) {

    override fun getItem(position: Int): Fragment {
        return pagers[position]
    }

    override fun getCount(): Int = pagers.count()

    override fun getPageTitle(position: Int): CharSequence? {
        return pagers[position].getTitle()
    }
}